<?php
global $currentTabId;
$date_reg = date('Y-m-d');
$start_date = intval( $date_reg) ? new CDate( dPgetParam($_REQUEST, 'log_start_date', date('Y-m-d') ) ) : null;
$end_date = intval( $date_reg) ? new CDate( dPgetParam($_REQUEST, 'log_end_date', date('Y-m-d') ) ) : null;

$df = $AppUI->getPref('SHDATEFORMAT');
if ($a = dPgetParam($_REQUEST, 'a', '') == ''){
	$a = "&tab={$currentTabId}&showdetails=1";
} else {
	$user_id = dPgetParam($_REQUEST, 'user_id', 0);
	$a = "&a=viewuser&user_id={$user_id}&tab={$currentTabId}&showdetails=1";
}
?>
<script languaje="JavaScript">
Calendar.enabled = true;

function doFormatDate(d)
{
	var d = Date.parseDate(d.substr(0,4)+'/'+d.substr(4,2)+'/'+d.substr(6,2), '%Y/%m/%d');
	return d.print('<?=$df?>');
}

function doCheckDate(c)
{
	var f = $('idDateStart');
	var t = $('idDateEnd');
	if (f.value > t.value) {
		if (c == f) {
			t.value = f.value;
			$('idDateEndD').value = doFormatDate(f.value);
		}
		else {
			f.value = t.value;
			$('idDateStartD').value = doFormatDate(t.value);
		}
	}
}
</script>
<form action="index.php?m=admin<?=$a?>" method="post" name="frmDate">
<table align="center" width="100%" class="infopanel">
	<tr align="center">
		<td nowrap="nowrap">
			<input type="submit" class="button" value="<?=$AppUI->_('View')?>"/>&nbsp;
		</td>
		<td nowrap="nowrap"><?=$AppUI->_('From')?></td>
		<td align="left">
			<input id="idDateStart" type="hidden" name="log_start_date"
				value="<?=$start_date ? $start_date->format( FMT_TIMESTAMP_DATE ) : ''?>"
				onchange="doCheckDate(this)"/>
			<input id="idDateStartD" type="text" name="start_date" style="width: 64px"
				value="<?=$start_date ? $start_date->format($df) : ''?>" class="text" disabled="disabled" />
		</td>
		<td>
			<img id="idDateStartB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>">
		</td>
		<td nowrap="nowrap">&nbsp;<?=$AppUI->_('to')?></td>
		<td align="left">
			<input id="idDateEnd" type="hidden" name="log_end_date"
				value="<?=$end_date ? $end_date->format( FMT_TIMESTAMP_DATE ) : ''?>"
				onchange="doCheckDate(this)"/>
			<input id="idDateEndD" type="text" name="end_date" style="width: 64px"
				value="<?=$end_date ? $end_date->format($df) : ''?>" class="text" disabled="disabled" />
		</td>
		<td>
			<img id="idDateEndB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>">
		</td>
		<td width="100%" align="left">
		</td>
	</tr>
</table>
</form>

<?php
if (dPgetParam($_REQUEST, 'showdetails', 0) == 1 ) {
	$start_date = date('Y-m-d', strtotime(dPgetParam($_REQUEST, 'log_start_date', date('Y-m-d') )));
	$end_date   = date('Y-m-d 23:59:59', strtotime(dPgetParam($_REQUEST, 'log_end_date', date('Y-m-d') )));

	$q  = new DBQuery;
	$q->addTable('user_access_log', 'ual');
	$q->addTable('users', 'u');
	$q->addTable('contacts', 'c');
	$q->addQuery('ual.*, u.*, c.*');
	$q->addWhere('ual.user_id = u.user_id');
	$q->addWhere('user_contact = contact_id ');
	if($user_id != 0) { $q->addWhere("ual.user_id='$user_id'"); }
	$q->addWhere("ual.date_time_in >='$start_date'");
	$q->addWhere("ual.date_time_out <='$end_date'");
	$q->addGroup('ual.date_time_last_action DESC');
	$logs = $q->loadList();
?>
<table align="center" class="list" width="100%">
<tr>
	<th nowrap="nowrap" width="80"><?=$AppUI->_('ID')?></th>
	<th nowrap="nowrap" ><?=$AppUI->_('Full Name')?></th>
	<th nowrap="nowrap" ><?=$AppUI->_('From Address')?></th>
	<th nowrap="nowrap" ><?=$AppUI->_('Date Time IN')?></th>
	<th nowrap="nowrap" class="last"><?=$AppUI->_('Date Time OUT')?></th>
</tr>
<?php $n = 1; foreach ($logs as $detail){?>
	<tr class="<? echo $n % 2 == 1 ? 'first' : 'second'; $n++; ?>">
		<td align="center"><?=$detail['user_username']?></td>
		<td align="center"><?=$detail['contact_first_name'].' '.$detail['contact_last_name']?></td>
		<td align="center"><?=$detail['user_ip']?></td>
		<td align="center"><?=$detail['date_time_in']?></td>
		<td align="center"><?=$detail['date_time_out']?></td>
	</tr>
<?php } ?>
</table>
<?php } ?>

